﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using StsServer.Data.Configuration;

namespace StsServer.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            //this.Users

            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

            // https://github.com/joaopgrassi/fluentvalidation-efcore-ruletesting/blob/master/src/Shop.Data/ShopDbContext.cs
            // Applies all the configurations for entities.
            // builder.ApplyConfigurationsFromAssembly(GetType().Assembly);
            //ApplyConfigurationsEntities(builder);

            foreach (var entity in builder.Model.GetEntityTypes())
            {
                // Replace table names
                entity.SetTableName(entity.GetTableName().ToSnakeCase());
                //entity.Relational().TableName = entity.Relational().TableName.ToSnakeCase();

                // Replace column names            
                //foreach (var property in entity.GetProperties())
                //{
                //    property.Relational().ColumnName = property.Relational().ColumnName.ToSnakeCase();
                //}

                //foreach (var key in entity.GetKeys())
                //{
                //    key.Relational().Name = key.Relational().Name.ToSnakeCase();
                //}

                //foreach (var key in entity.GetForeignKeys())
                //{
                //    key.Relational().Name = key.Relational().Name.ToSnakeCase();
                //}

                //foreach (var index in entity.GetIndexes())
                //{
                //    index.Relational().Name = index.Relational().Name.ToSnakeCase();
                //}
            }
        }

        private static void ApplyConfigurationsEntities(ModelBuilder modelBuilder)
        {
            
            modelBuilder.ApplyConfiguration(new RoleConfiguration());
            modelBuilder.ApplyConfiguration(new RoleClaimConfiguration());

            modelBuilder.ApplyConfiguration(new UserConfiguration());
            modelBuilder.ApplyConfiguration(new UserClaimConfiguration());
            modelBuilder.ApplyConfiguration(new UserTokenConfiguration());
            modelBuilder.ApplyConfiguration(new UserRoleConfiguration());
            modelBuilder.ApplyConfiguration(new UserLoginConfiguration());

        }



    }
}
